import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/services.dart';
import 'package:acore/components/date_time_picker/time_selector.dart';
import 'package:acore/components/date_time_picker/date_time_picker_translation_keys.dart';

void main() {
  group('TimeSelector Widget Tests', () {
    setUp(() {
      // Reset any global state before each test
      WidgetsFlutterBinding.ensureInitialized();
    });

    testWidgets('TimeSelector renders correctly with initial time', (WidgetTester tester) async {
      const initialTime = TimeOfDay(hour: 14, minute: 30);
      const selectedDate = '2024-01-15';

      await tester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            body: TimeSelector(
              selectedDate: DateTime.parse(selectedDate),
              initialTime: initialTime,
              showTimePicker: true,
              translations: {
                DateTimePickerTranslationKey.selectTimeTitle: 'Select Time',
                DateTimePickerTranslationKey.setTime: 'Set Time',
              },
              onTimeChanged: (DateTime newTime) {},
            ),
          ),
        ),
      );

      // Verify the component renders
      expect(find.byType(TimeSelector), findsOneWidget);
      expect(find.text('2:30 PM'), findsOneWidget);
      expect(find.byIcon(Icons.access_time), findsOneWidget);
    });

    testWidgets('TimeSelector has proper semantic labels for accessibility', (WidgetTester tester) async {
      const initialTime = TimeOfDay(hour: 9, minute: 15);
      const selectedDate = '2024-01-15';

      await tester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            body: TimeSelector(
              selectedDate: DateTime.parse(selectedDate),
              initialTime: initialTime,
              showTimePicker: true,
              translations: {
                DateTimePickerTranslationKey.selectTimeTitle: 'Select Time',
              },
              onTimeChanged: (DateTime newTime) {},
            ),
          ),
        ),
      );

      // Check for semantic button label
      expect(
        find.bySemanticsLabel('Selected time: 9:15 AM. Tap to change time.'),
        findsOneWidget,
      );
    });

    testWidgets('TimeSelector responds to tap gestures', (WidgetTester tester) async {
      const initialTime = TimeOfDay(hour: 12, minute: 0);
      const selectedDate = '2024-01-15';

      await tester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            body: TimeSelector(
              selectedDate: DateTime.parse(selectedDate),
              initialTime: initialTime,
              showTimePicker: true,
              translations: {
                DateTimePickerTranslationKey.selectTimeTitle: 'Select Time',
              },
              onTimeChanged: (DateTime newTime) {},
            ),
          ),
        ),
      );

      // Tap on the time selector
      await tester.tap(find.byType(TimeSelector));
      await tester.pump();

      // Verify the inline time picker appears
      expect(find.text('12:00 PM'), findsOneWidget);
    });

    testWidgets('TimeSelector handles keyboard navigation - Enter key', (WidgetTester tester) async {
      const initialTime = TimeOfDay(hour: 10, minute: 30);
      const selectedDate = '2024-01-15';

      await tester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            body: TimeSelector(
              selectedDate: DateTime.parse(selectedDate),
              initialTime: initialTime,
              showTimePicker: true,
              translations: {
                DateTimePickerTranslationKey.selectTimeTitle: 'Select Time',
              },
              onTimeChanged: (DateTime newTime) {},
            ),
          ),
        ),
      );

      // Focus the time selector and press Enter
      await tester.tap(find.byType(TimeSelector));
      await tester.pump();

      await tester.sendKeyEvent(LogicalKeyboardKey.enter);
      await tester.pump();

      // Verify interaction was handled
      expect(find.byType(TimeSelector), findsOneWidget);
    });

    testWidgets('TimeSelector handles keyboard navigation - Escape key', (WidgetTester tester) async {
      const initialTime = TimeOfDay(hour: 10, minute: 30);
      const selectedDate = '2024-01-15';

      await tester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            body: TimeSelector(
              selectedDate: DateTime.parse(selectedDate),
              initialTime: initialTime,
              showTimePicker: true,
              translations: {
                DateTimePickerTranslationKey.selectTimeTitle: 'Select Time',
              },
              onTimeChanged: (DateTime newTime) {},
            ),
          ),
        ),
      );

      // Focus the time selector and press Escape
      await tester.tap(find.byType(TimeSelector));
      await tester.pump();

      // First tap to open inline picker
      await tester.sendKeyEvent(LogicalKeyboardKey.enter);
      await tester.pump();

      // Then Escape to close
      await tester.sendKeyEvent(LogicalKeyboardKey.escape);
      await tester.pump();

      // Verify component still exists
      expect(find.byType(TimeSelector), findsOneWidget);
    });

    testWidgets('TimeSelector displays different time formats correctly', (WidgetTester tester) async {
      final testCases = [
        const TimeOfDay(hour: 0, minute: 0), // Midnight
        const TimeOfDay(hour: 12, minute: 0), // Noon
        const TimeOfDay(hour: 23, minute: 59), // Late night
        const TimeOfDay(hour: 9, minute: 5), // Single digit minute
      ];

      const selectedDate = '2024-01-15';

      for (final testTime in testCases) {
        await tester.pumpWidget(
          MaterialApp(
            home: Scaffold(
              body: TimeSelector(
                selectedDate: DateTime.parse(selectedDate),
                initialTime: testTime,
                showTimePicker: true,
                translations: {
                  DateTimePickerTranslationKey.selectTimeTitle: 'Select Time',
                },
                onTimeChanged: (DateTime newTime) {},
              ),
            ),
          ),
        );

        // Verify the component renders with different times
        expect(find.byType(TimeSelector), findsOneWidget);

        // Clear for next iteration
        await tester.pumpWidget(Container());
      }
    });

    testWidgets('TimeSelector handles null haptic feedback gracefully', (WidgetTester tester) async {
      const initialTime = TimeOfDay(hour: 14, minute: 30);
      const selectedDate = '2024-01-15';

      await tester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            body: TimeSelector(
              selectedDate: DateTime.parse(selectedDate),
              initialTime: initialTime,
              showTimePicker: true,
              translations: {
                DateTimePickerTranslationKey.selectTimeTitle: 'Select Time',
              },
              onTimeChanged: (DateTime newTime) {},
              // No haptic feedback callback provided
            ),
          ),
        ),
      );

      // Should render without issues
      expect(find.byType(TimeSelector), findsOneWidget);

      // Tap should work without crashing
      await tester.tap(find.byType(TimeSelector));
      await tester.pump();

      expect(find.byType(TimeSelector), findsOneWidget);
    });

    group('TimeSelector Accessibility Tests', () {
      testWidgets('TimeSelector meets WCAG contrast requirements', (WidgetTester tester) async {
        const initialTime = TimeOfDay(hour: 10, minute: 30);
        const selectedDate = '2024-01-15';

        await tester.pumpWidget(
          MaterialApp(
            home: Scaffold(
              body: TimeSelector(
                selectedDate: DateTime.parse(selectedDate),
                initialTime: initialTime,
                showTimePicker: true,
                translations: {
                  DateTimePickerTranslationKey.selectTimeTitle: 'Select Time',
                },
                onTimeChanged: (DateTime newTime) {},
              ),
            ),
          ),
        );

        // Verify semantic properties exist
        expect(
          find.bySemanticsLabel(RegExp(r'Selected time:.*Tap to change time.')),
          findsOneWidget,
        );
      });
    });

    group('TimeSelector Performance Tests', () {
      testWidgets('TimeSelector renders efficiently', (WidgetTester tester) async {
        const initialTime = TimeOfDay(hour: 14, minute: 30);
        const selectedDate = '2024-01-15';

        final stopwatch = Stopwatch()..start();

        await tester.pumpWidget(
          MaterialApp(
            home: Scaffold(
              body: TimeSelector(
                selectedDate: DateTime.parse(selectedDate),
                initialTime: initialTime,
                showTimePicker: true,
                translations: {
                  DateTimePickerTranslationKey.selectTimeTitle: 'Select Time',
                },
                onTimeChanged: (DateTime newTime) {},
              ),
            ),
          ),
        );

        stopwatch.stop();

        // Should render within reasonable time (less than 100ms)
        expect(stopwatch.elapsedMilliseconds, lessThan(100));
        expect(find.byType(TimeSelector), findsOneWidget);
      });
    });
  });
}
